package io.bdmc.modules.sys.service.impl;

import io.bdmc.modules.sys.mapper.SysRoleMenuMapper;
import io.bdmc.modules.sys.entity.SysRoleMenu;
import io.bdmc.modules.sys.service.SysRoleMenuService;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



/**
 * 角色与菜单对应关系
 * 
 * @author huang lu
 * @email hlkyo@qq.com
 * @date 2016年9月18日 上午9:44:35
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
	@Autowired
	private SysRoleMenuMapper rmMapper;

	@Override
	@Transactional
	public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
		//先删除角色与菜单关系
		QueryWrapper<SysRoleMenu> w1 = new QueryWrapper<SysRoleMenu>().eq("role_id", roleId);
		remove(w1);

		//保存角色与菜单关系
		if(menuIdList.size() == 0){
			return ;
		}
		List<SysRoleMenu> list = new ArrayList<>();
		for (Long m_id : menuIdList) {
			SysRoleMenu rm = new SysRoleMenu();
			rm.setMenuId(m_id);
			rm.setRoleId(roleId);
			list.add(rm);
		}
		saveBatch(list);
	}

	@Override
	public List<Long> queryMenuIdList(Long roleId) {
		return rmMapper.queryMenuIdList(roleId);
	}

}
